<?php
if (eregi(basename( __FILE__ ), $_SERVER['PHP_SELF'])) {
	print "<h1>Incorrect access</h1>You cannot access this file directly.";
	exit();
}

class rss {

    	function playme(){	
    		$code = AZLib::getParam('code');
			switch( $code ){			
				case 'user':				
					$this->build_user_rss();
					break;
				case 'latest':
					$this->build_latest_rss();
					break;					
				default:
					$this->build_rss();
					break;
			}
		}


function home()
{
	die("no_action");
}

	function build_latest_rss()
	{	
		$start_date = mktime(0,0,0,date('m',time()),date('d',time()),date('Y',time()));
		$end_date = mktime(23,59,59,date('m',time()),date('d',time()),date('Y',time()));
		
		/*$start_date = mktime(0,0,0,10,30,2008);
		$end_date = mktime(23,59,59,10,30,2008);*/
		
		$subDir='rss1';
		$cachefile = 'rss1_latest.';
			
		header("content-type: text/xml");	
		if(StaticCache::notExistCache($cachefile,3600,false,$subDir))
		{
			
			StaticCache::startCache();					
			$rss2 = '';		// content of rss2 file	
			// start:begin rss2		
			$rss2 = '<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel>';
			$rss2.= '<title> RSS Mới nhất - '.WEB_NAME.'</title>';
			$rss2.= '<description>RSS Mới nhất - '.WEB_NAME.'</description>';
			$rss2.='<link>'.WEB_ROOT.'</link>';
			$rss2.= '<copyright>'.WEB_NAME.': http://'.strtolower(WEB_NAME).' , Powered  by AzNet Team: contact@aznetgroup.com</copyright>';
			$rss2.= '<docs>http://backend.userland.com/rss</docs>';
			$rss2.= '<generator>'.WEB_NAME.':http://'.strtolower(WEB_NAME).'/rss.html</generator>';
			$rss2.= '<image><title>'.WEB_NAME.'</title><url>'.WEB_ROOT.'style/img/header/logo.png</url><link>http://'.strtolower(WEB_NAME).'</link><width>150</width><height>83</height></image>';	
										
			$where_item = "item.status = 1 AND created_time BETWEEN $start_date AND $end_date";
					
			// list item	
			$sql = "SELECT item.id, category_id, item.name, created_time, item.description
					FROM item, category 
					WHERE category.id = item.category_id AND $where_item
					ORDER BY created_time DESC";
			$re = DB::query($sql);	
			if($re){
			while ($item=mysql_fetch_assoc($re)){
					$page = 'item_detail';
					$item['name'] = trim(XMLLib::_xml_encode_attribute(strip_tags(html_entity_decode($item['name']))));
					$item['description'] = trim(XMLLib::_xml_encode_attribute(strip_tags(html_entity_decode(AZLib::word_limit($item['description'],150), ENT_QUOTES, "UTF-8"))));				
					$item['url'] = WEB_ROOT."p".$item['id']."/".AZLib::safe_title($item['name']).'.html';					
					$rss2.= "<item>";
					$rss2.= "<title>".$item['name']." - ".WEB_NAME."</title>";
					$rss2.= "<description>(".WEB_NAME.") - ".$item['description']."</description>";
					$rss2.= "<link>".$item['url']."</link>";
					$rss2.= '<guid isPermaLink="True">'.$item['url'].'</guid>';
					$rss2.= "<pubDate>".date("D, d M Y H:i:s",$item['created_time'])." GMT</pubDate>";
					$rss2.= "</item>";
				}
			}
			//end list item
			
			// list shop	
			$where_shop = "is_active = 0 AND create_time BETWEEN $start_date AND $end_date";	
			$sql = 'SELECT user_name, email, show_email, create_time FROM user WHERE '.$where_shop.' ORDER BY id DESC';
			
			$re = DB::query($sql);	
			if($re){					
				while($user = mysql_fetch_array($re)){
					$str_email = '';			
					if($user['show_email']){
						$str_email .= ', Email: '.$user['email'];
					}
					$user['url'] = WEB_ROOT.$user['user_name'];
					$user['description'] = 'Tên truy cập: '.$user['user_name'].', Ngày tham gia Én Bạc: '.date("d M Y H:i:s",$user['create_time']).$str_email;
					
					$rss2.= "<item>";
					$rss2.= "<title>".$user['user_name']." - ".WEB_NAME."</title>";
					$rss2.= "<description>(".WEB_NAME.") - ".$user['description']."</description>";
					$rss2.= "<link>".$user['url']."</link>";
					$rss2.= '<guid isPermaLink="True">'.$user['url'].'</guid>';
					$rss2.= "<pubDate>".date("D, d M Y H:i:s",$user['create_time'])." GMT</pubDate>";
					$rss2.= "</item>";
				}		
			}	
			//end list shop	
			
			$rss2.= '</channel></rss>';				
			
			echo $rss2;
			StaticCache::endCache();
		}
		
		return  '';
	}

	function build_rss()
	{
		$arr_cat = array(293);// dinh nghia mang cho cac danh muc co so luong tin nhieu
		$limit = 100;// mac dinh ban dau
		$limits = 200;// so luong ban ghi cho danh muc co nhieu tin
		
		$category_name = AZLib::getParam('ebname');	
		$category_id = AZLib::getParam('cat_id');
	
		AZLib::getCats();
	
		if($category_name!="" && $category_id=="" ){
			foreach(CGlobal::$allCategories as $cat){
				if($cat['parent_id']==0 && AZLib::safe_title($cat['name'])==$category_name){
					$category = $cat;
					break;
				}
			}		
			header("Location:".WEB_ROOT."rss/c".$category['id']."_".$category_name.".rss");
		}			
		
		foreach(CGlobal::$allCategories as $cat)
		{		
			if($cat['parent_id']==0 && $cat['id']==$category_id)
			{
				$category = $cat;
				break;
			}
		}
		
	
		if($category)
		{
			$cat_id = $category['id'];
			$cat_name = trim(XMLLib::_xml_encode_attribute($category['name']));
			
			$subDir='rss1';
			$cachefile = 'rss1_'.$cat_id;	
			header("content-type: text/xml");
			if(StaticCache::notExistCache($cachefile,3600,false,$subDir))
			{	
				if(in_array($cat_id,$arr_cat)){
					$limit = $limits;
				}	
				StaticCache::startCache();		
				$rss = $this->build_rss_cat($cat_id,$cat_name,$limit);	
				echo $rss;
				StaticCache::endCache();
			}
			//echo 'a';
		}
		else
		{
			REWRITE_ON ? header("Location:".WEB_ROOT."rss/") : header("Location:".WEB_ROOT."?page=rss");
		}
		System::halt();
	}
	
	function build_user_rss()
	{			
		$user_id = (int)AZLib::getParam('user_id');
		$user = User::getUser($user_id);							
		if($user)
		{		
		
			$subDir='rss1/'.floor($user_id/1000);
			//echo 
			$user_id = $user['id'];
			
			$user['full_name'] != '' ? $user_name = trim(XMLLib::_xml_encode_attribute($user['full_name'])) : $user_name = trim(XMLLib::_xml_encode_attribute($user['user_name']));
	
			$cachefile = 'rss1_user_'.$user_id;
			header("content-type: text/xml");
			
			
			if(StaticCache::notExistCache($cachefile,3600,false,$subDir))
			{
				StaticCache::startCache();
				$user_name = "Tin &#273;&#259;ng c&#7911;a ".$user_name;
				$rss = $this->build_rss_cat(0,$user_name,100,$user_id);		
				echo $rss;
				StaticCache::endCache();
			}
			
		}
		else
		{
			REWRITE_ON ? header("Location:".WEB_ROOT."rss/") : header("Location:".WEB_ROOT."?page=rss");		
		}
		System::halt();
	}

	function build_rss_cat($cat_id='0',$cat_name='home',$limit=100,$user_id=false)
	{				
		$rss2 = '';		// content of rss2 file
		// start:begin rss2
		$rss2 = '<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel>';
		$rss2.= '<title>'.$cat_name.' - '.WEB_NAME.'</title>';
		$rss2.= '<description>'.$cat_name.' - '.WEB_NAME.'</description>';
		//$rss2.= '<link>'.trim(XMLLib::_xml_encode_attribute(WEB_ROOT.'?page=list_detail&category_id='.$cat_id)).'</link>';
		//$cat_id ? $rss2.= '<link>'.WEB_ROOT.'?page=list_detail&category_id='.$cat_id.'</link>' : $rss2.='<link>'.WEB_ROOT.'?page=list_detail</link>';
		$rss2.= '<link>'.WEB_ROOT.'</link>' ;
		//$rss2.= '<link>'.WEB_ROOT.'?page=list_detail&category_id='.$cat_id.'</link>';
	
		$rss2.= '<copyright>'.WEB_NAME.': http://'.strtolower(WEB_NAME).' , Powered  by AzNet Team: contact@aznetgroup.com</copyright>';
		$rss2.= '<docs>http://backend.userland.com/rss</docs>';
		$rss2.= '<generator>'.WEB_NAME.':http://'.strtolower(WEB_NAME).'/rss.html</generator>';
		$rss2.= '<image><title>'.WEB_NAME.'</title><url>'.WEB_ROOT.'style/img/header/logo.png</url><link>http://'.strtolower(WEB_NAME).'</link><width>150</width><height>83</height></image>';
		
		$user_id ? $sql = "SELECT item.id, category_id, item.name, item.description, item.created_time 
							FROM item WHERE status=1 AND user_id=".$user_id." 
							ORDER BY created_time DESC LIMIT 0,25"
					: $sql = "SELECT item.id, category_id, item.name, item.description, item.created_time 
							FROM item, category 
							WHERE category.id = item.category_id AND item.status=1 
							AND (category_id='".$cat_id."' OR category.parent_id='".$cat_id."') 
							ORDER BY created_time DESC LIMIT 0,".$limit;					
		$re = DB::query($sql);
		if($re){
			while ($item=mysql_fetch_assoc($re)){
				$page = 'item_detail';
				$item['name'] = trim(XMLLib::_xml_encode_attribute(strip_tags(html_entity_decode($item['name']))));
				$item['description'] = trim(XMLLib::_xml_encode_attribute(strip_tags(html_entity_decode(AZLib::word_limit($item['description'],150), ENT_QUOTES, "UTF-8"))));
				//$item['url'] = trim(XMLLib::_xml_encode_attribute(WEB_ROOT."?page=".$page."&id=".$item['id']."&category_id=".$item['category_id']."&ebname=".AZLib::safe_title($item['name'])));
				$item['url'] = WEB_ROOT."p".$item['id']."/".AZLib::safe_title($item['name']).".html";	
				
				//$item['url'] = AZLib::remove_4_js($item['url']);
				//rss2
				$rss2.= "<item>";
				$rss2.= "<title>".$item['name']." - ".WEB_NAME."</title>";
				$rss2.= "<description>(".WEB_NAME.") - ".$item['description']."</description>";
				$rss2.= "<link>".$item['url']."</link>";
				$rss2.= '<guid isPermaLink="True">'.$item['url'].'</guid>';
				$rss2.= "<pubDate>".date("D, d M Y H:i:s",$item['created_time'])." GMT</pubDate>";
				$rss2.= "</item>";
			}
		}
		//content:end
		
		// end:begin
		$rss2.= '</channel></rss>';
		// end:end
	
		return  $rss2;
	}
}//class
?>